<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

require_once 'Zend/Controller/Action.php';
require_once APPLICATION_PATH . '/models/forms/AbmClientesForm.php';

/**
 * Description of ClientesController
 *
 * @author kristian
 */
class ClientesController extends Zend_Controller_Action{
    private $form;

    public function init(){

    }

    public function ClientesController(){
        $this->form = new AbmClientesForm();
    }

    /**
     * Pre-Dispatch: set up dojo and context switching
     *
     * @return void
     */
    public function preDispatch()
    {
        $request = $this->getRequest();

        Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
        $contextSwitch = $this->_helper->contextSwitch;
        if (!$contextSwitch->hasContext('ajax')) {
            $contextSwitch->addContext('ajax', array('suffix' => 'ajax'))
                        ->addActionContext('new', 'ajax')
                        ->addActionContext('followup', 'ajax')
                        ->addActionContext('display', 'ajax')
                        ->addActionContext('active', 'ajax')
                        ->addActionContext('active-data', 'ajax')
                        ->addActionContext('active-data-count', 'ajax')
                        ->initContext();
        }

        $message = array(
            'Current request information',
            array(
                array('Module', 'Controller', 'Action'),
                array($request->getModuleName(), $request->getControllerName(), $request->getActionName()),
            )
        );

        Zend_Registry::get('log')->table($message);
    }

    public function indexAction(){
        $this->view->title = "Administracion de clientes";
        $this->view->headTitle($this->view->title, 'PREPEND');
        $cliente = new Spalm_Model_Single_ClienteEntity();
        $this->view->clientes = $cliente->fetchAll();

        $clientes = $cliente->fetchAll();
        $datos = array();

        foreach ($clientes as $clienteActual) {
            $datos[$clienteActual->getPK()] = $clienteActual->getRazonSocial();
        }

        $datosLista = array('clientes',$datos);

        $this->form->populate($datosLista);
    }

    public function crearClienteAction(){
        $this->view->title = "Agregar clientes";
        $this->view->headTitle($this->view->title, 'PREPEND');

        $this->form->submit->setLabel('Agregar');
        $this->view->form = $this->form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->_request->getPost();
            if ($this->form->isValid($formData)) {
                $cliente = new Spalm_Model_Single_ClienteEntity();
                $localidad = new Spalm_Model_Single_LocalidadEntity();

                //arma el objeto de localidades
                $localidad = $localidad->find((int) $this->form->getValue('localidad'));

                $cliente->setDomicilio($this->form->getValue('domicilio'));
                $cliente->setEmail($this->form->getValue('email'));
                $cliente->setNroTelefono($this->form->getValue('nroTelefono'));
                $cliente->setNumeroDocumento($this->form->getValue('nroDocumento'));
                $cliente->setRazonSocial('razonSocial');
                $cliente->setTipoDocumento($this->form->getValue('tipoDocumento'));
                $cliente->setLocalidad($localidad);


                $cliente->save();

                //$this->_redirect('/');
            } else {
                $this->form->populate($formData);
            }
        }

    }

    public function modificarClienteAction(){
        $this->view->title = "Modificar clientes";
        $this->view->headTitle($this->view->title, 'PREPEND');

        $this->form->submit->setLabel('Modificar');
        $this->view->form = $this->form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->_request->getPost();
            if ($this->form->isValid($formData)) {
                $cliente = new Spalm_Model_Single_ClienteEntity();
                $localidad = new Spalm_Model_Single_LocalidadEntity();

                //arma el objeto de localidades
                $localidad = $localidad->find((int) $this->form->getValue('localidad'));
                $cliente = $cliente->find( (int) $this->form->getValue('cliente'));

                $cliente->setDomicilio($this->form->getValue('domicilio'));
                $cliente->setEmail($this->form->getValue('email'));
                $cliente->setNroTelefono($this->form->getValue('nroTelefono'));
                $cliente->setNumeroDocumento($this->form->getValue('nroDocumento'));
                $cliente->setRazonSocial('razonSocial');
                $cliente->setTipoDocumento($this->form->getValue('tipoDocumento'));
                $cliente->setLocalidad($localidad);


                $cliente->save();

                //$this->_redirect('/');
            } else {
                $this->form->populate($formData);
            }
        }
    }

    public function eliminarClienteAction(){
        $this->view->title = "Agregar clientes";
        $this->view->headTitle($this->view->title, 'PREPEND');

        $this->form->submit->setLabel('Eliminar');
        $this->view->form = $this->form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->_request->getPost();
            if ($this->form->isValid($formData)) {
                // recupera un arreglo con el seleccionado
                $seleccionado = $this->form->getValue('clientes');

                // obtiene la clave del arreglo (es la pk)
                foreach ($seleccionado as $pk => $razonSocial) {
                    $clave = $pk;
                }

                $cliente = new Spalm_Model_Single_ClienteEntity();
                $cliente = $cliente->find( (int) $clave);

                $cliente->remove();

                //$this->_redirect('/');
            } else {
                $this->form->populate($formData);
            }
        }
    }

    public function consultarClientesAction(){
        $this->view->title = "Consultar clientes";
        $this->view->headTitle($this->view->title, 'PREPEND');

        $this->form->submit->setLabel('Buscar');
        $this->view->form = $this->form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->_request->getPost();
            if ($this->form->isValid($formData)) {
                // recupera un arreglo con el seleccionado
                $seleccionado = $this->form->getValue('clientes');

                // obtiene la clave del arreglo (es la pk)
                foreach ($seleccionado as $pk => $razonSocial) {
                    $clave = $pk;
                }

                $cliente = new Spalm_Model_Single_ClienteEntity();
                $cliente = $cliente->find( (int) $clave);

                $this->form->populate($cliente->getArray());

                // $this->_redirect('/');
            } else {
                $this->form->populate($formData);
            }
        }
    }
}
?>
